package com.mindframe.pruebasmapas;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	
	private TextView lblLongitud;
	private TextView lblLatitud;
	private TextView lblEstado;
	private Button btnActivar;
	private Button btnDesActivar;
	private Button btnGuardar;
	private Button btnListaPuntos;
	
	private LocationManager locManager;
	private LocationListener locListener;
	Location location;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        lblLongitud = (TextView)findViewById(R.id.lblLongitud);
        lblLatitud = (TextView)findViewById(R.id.lblLatitud);
        lblEstado = (TextView)findViewById(R.id.lblEstado);
    	btnActivar = (Button)findViewById(R.id.btnActivar);
    	btnDesActivar = (Button)findViewById(R.id.btnDesactivar);
    	btnGuardar = (Button)findViewById(R.id.btnGuardar);
    	btnListaPuntos = (Button)findViewById(R.id.btnListaPuntos);
    	
    	btnListaPuntos.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(MainActivity.this, ListaPuntosActivity.class);
				startActivity(intent);
			}
		});
    	
    	btnActivar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				comenzarLocalizacion(); 
				
			}
		});
    	
    	btnDesActivar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(locListener != null)
					locManager.removeUpdates(locListener);
				
				lblLongitud.setText("Longitud: Sin datos");
				lblLatitud.setText("Latitud: Sin datos");
				lblEstado.setText("-");
				btnGuardar.setVisibility(View.INVISIBLE);
			}
		});
    	
    	btnGuardar.setVisibility(View.INVISIBLE);
    	
    }

	private void comenzarLocalizacion() {
		locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		
		location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		mostrarPosicion(location);
		
		btnGuardar.setVisibility(View.VISIBLE);
		
		locListener = new LocationListener() {
			
			@Override
			public void onProviderEnabled(String provider) {
				lblEstado.setText("Provider ON");
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				lblEstado.setText("Provider OFF");
				
			}
			
			@Override
			public void onLocationChanged(Location newLocation) {
				location = newLocation;
				mostrarPosicion(newLocation);
			}

			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				lblEstado.setText("Provider Status: " + status);
				
			}
		};
		
		locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locListener);
		
		btnGuardar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				guardarPunto(location);				
			}

		});
		
	}
	
	private void mostrarPosicion(Location location){
		if(location != null){
			lblLongitud.setText("Longitud: " + String.valueOf(location.getLongitude()));
			lblLatitud.setText("Latitud: " +  String.valueOf(location.getLatitude()));
			lblEstado.setText(String.valueOf(location.getAccuracy()));
			
		}else{
			//Si no tenemos los datos, ponemos los campos sin datos.
			lblLongitud.setText("Longitud: Sin datos");
			lblLatitud.setText("Latitud: Sin datos");
			lblEstado.setText("-");
			
			
		}
		
	}
	
	private void guardarPunto(Location location) {
		if(location != null){
			
			Intent intent = new Intent(MainActivity.this, SavePointActivity.class);
			Bundle bundle = new Bundle();
			
			bundle.putString("latitud", String.valueOf(location.getLatitude()));
			bundle.putString("longitud", String.valueOf(location.getLongitude()));
			bundle.putString("metodo", "guardar");
			intent.putExtras(bundle);
			
			startActivity(intent);
			
			
		}else{
			crearDialogoAlerta("Todavía no se ha recibido ningún punto");
			
		}
	}
	
	private void crearDialogoAlerta(String mensaje) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Aviso");
		alertDialog.setMessage(mensaje);
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		   public void onClick(DialogInterface dialog, int which) {
		      // here you can add functions
		   }
		});
		alertDialog.show();
	}
    
}
